package com.cff.shorturl.task;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cff.shorturl.util.StringUtil;

public abstract class CallbackTask implements Callable<String> {
	final Logger logger = LogManager.getLogger(CallbackTask.class);
	public static final String FAILED = "500";
	public static final String SUCESS = "200";
	Exception e = null;
	Future<String> result = null;

	@Override
	public String call() {
		try {
			doTask();
			return SUCESS;
		} catch (Exception e) {
			this.e = e;
			return FAILED;
		}
	}

	public void setResult(Future<String> result) {
		this.result = result;
	}

	public abstract void doTask() throws Exception;

	public void onFailure(Exception e) {
		logger.info("某次的失败了：{}", e.getMessage());
	}

	public void onSuccess() {
		logger.info("已保存成功。");
	}

	public String getResult() throws Exception {	
		String ret = result.get();
		if(!StringUtil.isEmpty(ret)){
			if(ret.equals(SUCESS)){
				onSuccess();
			}else{
				onFailure(e);
			}
		}
		return ret;
	}

}
